<template>
    <view class="p30">
        <view class="df jsb ac mb-20">
            <view class="box-left">县级短期预警</view>
        </view>
        <view class="cards df jsb mb-30 ac" v-for="(item, index) in YjybList" :key="index">
            <view>
                <view class="fs-32 fwb">{{ item.Name }}</view>
                <view>{{ item.CreateTime }}</view>
            </view>

            <view class="df" @click="downloadFile(item)" style="flex-shrink: 0;color:#2e5be3">
                <uv-icon name="list" color="#2e5be3" size="30rpx"></uv-icon>
                <view class="text">查看</view>
            </view>
        </view>
        <view class="df jsb ac mb-20">
            <view class="box-left">县级气象预警</view>
        </view>
        <view class="cards df jsb mb-30 list" v-for="(item, index) in BylzList" :key="index">
            <view>
                <view class="fs-32 fwb">{{ item.Name }}</view>
                <view>{{ item.CreateTime }}</view>
            </view>

            <view class="df" @click="downloadFile(item)" style="flex-shrink: 0;color:#2e5be3">
                <uv-icon name="list" color="#2e5be3" size="30rpx"></uv-icon>
                <view class="text">查看</view>
            </view>
        </view>
    </view>
</template>

<script lang="ts" setup>
import { GetPageFile_API } from '@/server/home';
import { getLoginMsg } from '@/utils/util';
import dayjs from 'dayjs';

onLoad(() => {
    getList(1)
    getList(2)
})
const YjybList = ref([])
const BylzList = ref([])
const getList = async (type) => {
    const res = await GetPageFile_API({
        AdminCode: getLoginMsg()?.AdminCode,
        TypeCode: type === 1 ? 'DQYJ' : 'QXYJ',
        pageSize: 9999
    })
    if (res.IsSuccess) {
        if (type === 1) {
            YjybList.value = res?.Data?.Data?.reverse().map(el => ({ ...el, CreateTime: dayjs(el.CreateTime).format('MM/DD HH时') }))
        } else {
            BylzList.value = res?.Data?.Data?.reverse().map(el => ({ ...el, CreateTime: dayjs(el.CreateTime).format('MM/DD HH时') }))
        }
    } else {
        if (type === 1) {
            YjybList.value = []
        } else {
            BylzList.value = []
        }
    }
}
// 预警预报
const downloadFile = (item) => {
    console.log("item:", item);
    if (['png', 'jpeg', 'jpg'].includes(item.FileType)) {
        //图片预览
        uni.previewImage({
            urls: [import.meta.env.LH_FILE_BASEURLDZFZ + item.Id],
        })
    } else if (['pdf', 'doc', 'docx', 'xls', 'xlsx', 'ppt', 'pptx'].includes(item.FileType)) {
        //文件预览
        uni.downloadFile({
            url: import.meta.env.LH_FILE_BASEURLDZFZ + item.Id,
            success: function (res) {
                var filePath = res.tempFilePath
                uni.openDocument({
                    filePath: filePath,
                    showMenu: true,
                    success: function (res) {
                        console.log("打开文档成功")
                    },
                })
            },
        })
    }
}
</script>

<style lang="scss" scoped>
.list {
    align-items: baseline;
}
</style>
